package com.selune.online.ordering.system.project.api.base.response;

import java.io.Serializable;
import lombok.Data;

/**
 * 返回给前台的通用包装
 *
 * @author xiaoyp
 * @date 2020/2/27
 */

@Data
public class Response<T> implements Serializable {
	private static final long serialVersionUID = 684197548184588396L;
	
	private static final String DEFAULT_SUCCESS_MESSAGE = "请求成功";
	
	private static final String DEFAULT_ERROR_MESSAGE = "请求失败";
	
	private static final Integer DEFAULT_SUCCESS_CODE = 200;
	
	private static final Integer DEFAULT_ERROR_CODE = 400;
	
	/**
	 * 响应状态码
	 */
	private Integer code;
	
	/**
	 * 响应信息
	 */
	private String message;
	
	/**
	 * 错误信息
	 */
	private String error;
	
	/**
	 * 响应对象
	 */
	private T data;
	
	/**
	 * 时间戳
	 */
	private Long time;
	
	/**
	 * 路径
	 */
	private String path;
	
	public Response() {
	}
	
	public Response(Integer code, String message, String error, T data, Long time, String path) {
		this.code = code;
		this.message = message;
		this.error = error;
		this.data = data;
		this.time = time;
		this.path = path;
	}
	
	public static <T> Response<T> success() {
		return new <T>Response<T>();
	}
	
	public static <T> Response<T> success(T data) {
		return new <T>Response<T>(
				DEFAULT_SUCCESS_CODE,
				DEFAULT_SUCCESS_MESSAGE,
				null,
				data,
				System.currentTimeMillis(),
				null
		);
	}
	
	public static <T> Response<T> success(String message, T data) {
		return new <T>Response<T>(DEFAULT_SUCCESS_CODE,
				message,
				null,
				data,
				System.currentTimeMillis(),
				null
		);
	}
	
	public static <T> Response<T> success(Integer code, String message, T data) {
		return new <T>Response<T>(code,
				message,
				null,
				data,
				System.currentTimeMillis(),
				null
		);
	}
	
	public static <T> Response<T> error(String message) {
		return new <T>Response<T>(
				DEFAULT_ERROR_CODE,
				message,
				null,
				null,
				System.currentTimeMillis(),
				null
		);
	}
	
	public static <T> Response<T> error(Integer code, String message) {
		return new <T>Response<T>(
				code,
				message,
				null,
				null,
				System.currentTimeMillis(),
				null
		);
	}
	
	public static <T> Response<T> error(Integer code, String message, T data) {
		return new <T>Response<T>(code,
				message,
				null,
				data,
				System.currentTimeMillis(),
				null
		);
	}
	
	public static <T> Response<T> error(Integer code, String message, String error, T data, String path) {
		return new <T>Response<T>(
				code,
				message,
				error,
				data,
				System.currentTimeMillis(),
				path
		);
	}
}
